package leetcode.n2022;

import cn.hutool.log.Log;
import cn.hutool.log.LogFactory;

import java.util.Arrays;

/**
 * https://leetcode-cn.com/problems/convert-1d-array-into-2d-array/
 * @program: brushing
 * @description:
 * @author: LiuQiang
 * @create: 2022-01-01
 **/
public class Construct2DArray {

    public int[][] construct2DArray(int[] original, int m, int n) {

        int [][] result;

        // 先判断是否能 组成
        if (original.length != m * n) {
            result = new int[0][];
            return result;
        }

        // 开始拼接
        result = new int[m][n];

        for (int i = 0; i < original.length; i++) {
            result[i / n][i % n] = original[i];
        }

        return result;
    }

    public static void main(String[] args) {
        Log log = LogFactory.get();
        Construct2DArray construct2DArray = new Construct2DArray();

        int[] a = new int[]{1,2,3,4,5,6,7,8};
        int m = 2;
        int n = 4;

        int [][]result = construct2DArray.construct2DArray(a,m,n);

        for (int i = 0; i < result.length ; i++) {
            for (int j = 0; j < result[i].length; j++) {
                System.out.print(result[i][j]+"\t");
            }

        }
    }
}
